﻿Public Class Frm_Login

    Private Sub Frm_Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Load config
        Call InisialisasiAplikasi()

    End Sub

    Private Sub ButtonExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        End
    End Sub

    Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click

        '# Proses inisialisasi validasi
        Validation.clearRules()
        Validation.addRules(txt_Username.Text, "Username", "required")
        Validation.addRules(txt_Password.Text, "Password", "required")

        '# cek validasi
        If Validation.isValid Then

            '# init query
            Dim query As String = vbNullString
            query &= " SELECT * FROM tbl_user "
            query &= " WHERE user = '" & txt_Username.Text & "' AND "
            query &= "       pass = MD5('" & txt_Password.Text & "') AND "
            query &= "       type = '" & cmb_UserType.SelectedIndex & "'  "

            '# eksekusi query kedalam datatable
            Dim dt As DataTable = Connection.ExecuteToDataTable(query)

            '# cek user 
            If dt.Rows.Count > 0 Then

                Auth.User = txt_Username.Text
                Auth.nim = dt.Rows(0).Item("nim").ToString
                Auth.Tipe = cmb_UserType.SelectedIndex
                Auth.TipeDeskripsi = IIf(cmb_UserType.SelectedIndex = 1, "Admin", "Guest")

                Frm_Main.Show()

                'clean textbox
                txt_Username.Text = ""
                txt_Password.Text = ""
                Me.Hide()
            Else
                MsgBox("Login gagal. Pastikan Username dan Password telah benar", MsgBoxStyle.Exclamation, "Login")
            End If

        Else
            Validation.showMessage()
        End If

    End Sub
End Class